<?php

namespace App\EventSubscriber;

use App\Entity\Notice;
use App\Event\NoticeEvent;
use App\Utils\Helper\GetterHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * 消息通知事件订阅
 * Class NoticeSubscriber
 * @package App\EventSubscriber
 */
class NoticeSubscriber implements EventSubscriberInterface {

    protected $function_prefix = 'NoticeType_';
    protected $event;
    protected $logger;

    public function __construct (LoggerInterface $logger) {
        $this->logger = $logger;
    }

    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents () {
        return [
            NoticeEvent::NAME => [
                ['buildNotice', 10],
            ],
        ];
    }

    /**
     * build notice
     * @param NoticeEvent $event
     */
    public function buildNotice (NoticeEvent $event) {
        $this->event = $event;
        $notice_function = $this->function_prefix.$event->getNoticeTypeId();
        if (method_exists($this, $notice_function)) {
            $this->$notice_function();
        }
    }

    /**
     * 获取当前事件对象
     * @return NoticeEvent
     */
    private function getEvent () {
        return $this->event;
    }

    /**
     * 系统消息
     */
    private function NoticeType_1 () {
        try {
            $notice = new Notice();
            $notice->setNoticeTypeId($this->getEvent()->getNoticeTypeId());
            $notice->setTitle($this->getEvent()->getTitle());
            $notice->setContent($this->getEvent()->getContent());
            $notice->setFromType($this->getEvent()->getFromType());
            $notice->setFromId($this->getEvent()->getFromId());
            $notice->setToType($this->getEvent()->getToType());
            $notice->setToId($this->getEvent()->getToId());
            GetterHelper::getEntityManager()->persist($notice);
            GetterHelper::getEntityManager()->flush();
        } catch (\Error $e) {
            $this->logger->error('消息通知事件 -> 系统消息 -> 失败. Error：'.$e->getMessage());
        } catch (\Exception $e) {
            $this->logger->error('消息通知事件 -> 系统消息 -> 失败. Exception：'.$e->getMessage());
        }
    }

    /**
     * 审批消息
     */
    private function NoticeType_2 () {
        try {
            $notice = new Notice();
            $notice->setNoticeTypeId($this->getEvent()->getNoticeTypeId());
            $notice->setTitle($this->getEvent()->getTitle());
            $notice->setContent($this->getEvent()->getContent());
            $notice->setFromType($this->getEvent()->getFromType());
            $notice->setFromId($this->getEvent()->getFromId());
            $notice->setToType($this->getEvent()->getToType());
            $notice->setToId($this->getEvent()->getToId());
            $notice->setAdminUserId($this->getEvent()->getAdminUserId());
            GetterHelper::getEntityManager()->persist($notice);
            GetterHelper::getEntityManager()->flush();
        } catch (\Error $e) {
            $this->logger->error('消息通知事件 -> 审批消息 -> 失败. Error：'.$e->getMessage());
        } catch (\Exception $e) {
            $this->logger->error('消息通知事件 -> 审批消息 -> 失败. Exception：'.$e->getMessage());
        }
    }

    /**
     * 工单消息
     */
    private function NoticeType_3 () {
        try {
            $notice = new Notice();
            $notice->setNoticeTypeId($this->getEvent()->getNoticeTypeId());
            $notice->setTitle($this->getEvent()->getTitle());
            $notice->setContent($this->getEvent()->getContent());
            $notice->setFromType($this->getEvent()->getFromType());
            $notice->setFromId($this->getEvent()->getFromId());
            $notice->setToType($this->getEvent()->getToType());
            $notice->setToId($this->getEvent()->getToId());
            $notice->setAdminUserId($this->getEvent()->getAdminUserId());
            GetterHelper::getEntityManager()->persist($notice);
            GetterHelper::getEntityManager()->flush();
        } catch (\Error $e) {
            $this->logger->error('消息通知事件 -> 工单消息 -> 失败. Error：'.$e->getMessage());
        } catch (\Exception $e) {
            $this->logger->error('消息通知事件 -> 工单消息 -> 失败. Exception：'.$e->getMessage());
        }
    }

}